package com.kenyi.co.ui.me.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnNetWorkErrorListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.common.ui.BaseActivity;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.okhttp.utils.GsonResultok;
import com.kenyi.co.ui.me.adapter.CollectAdapter;
import com.kenyi.co.ui.me.adapter.MainFootAdapter;
import com.kenyi.co.ui.me.bean.MyFootBean;
import com.kenyi.co.ui.video.VideoDetailActivity;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.SharepUtils;
import com.kenyi.co.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


/**
 * 设置头像
 */
public class MyFootActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "MyFootActivity";

    private Button li_footer;
    private TextView tv_home_title;
    private TextView tv_no_data;

    private Toolbar toolbar;

    private List<MyFootBean.DatasBean.FootArticlesBean> itemList;
    private int pos = 0;

    private LRecyclerView mRecyclerView = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;

    private MainFootAdapter mDataAdapter = null;
    private View mEmptyView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_foot);
        initView();
        mEmptyView = findViewById(R.id.empty_view);
        tv_no_data = findViewById(R.id.tv_no_data);
        mRecyclerView = (LRecyclerView) findViewById(R.id.list);
        mDataAdapter = new MainFootAdapter(MyFootActivity.this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);


        mRecyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //是否禁用自动加载更多功能,false为禁用, 默认开启自动加载更多功能
        mRecyclerView.setLoadMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);
        mDataAdapter.setOnDelListener(new CollectAdapter.onSwipeListener() {
            @Override
            public void onDel(int pos) {

                getDeleteCollectActicles(itemList.get(pos).getId());
                //RecyclerView关于notifyItemRemoved的那点小事 参考：http://blog.csdn.net/jdsjlzx/article/details/52131528
                mDataAdapter.getDataList().remove(pos);
                mDataAdapter.notifyItemRemoved(pos);//推荐用这个
                itemList.remove(pos);
                if (pos != (mDataAdapter.getDataList().size())) { // 如果移除的是最后一个，忽略 注意：这里的mDataAdapter.getDataList()不需要-1，因为上面已经-1了
                    mDataAdapter.notifyItemRangeChanged(pos, mDataAdapter.getDataList().size() - pos);
                }
                //且如果想让侧滑菜单同时关闭，需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
            }

        });
        mRecyclerView.setOnNetWorkErrorListener(new OnNetWorkErrorListener() {
            @Override
            public void reload() {

            }
        });
        mRecyclerView.refresh();

        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                MyFootBean.DatasBean.FootArticlesBean item = mDataAdapter.getDataList().get(position);
//                Intent metags = new Intent(MyFootActivity.this, VideoDetailActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("id", item.getId() + "");
////                bundle.putString("id", "1907030516");
//                metags.putExtras(bundle);
//                startActivity(metags);

            }
        });
        getFootActicles();
    }

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.white));//设置状态栏颜色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        }
    }
    private void initView() {
        setStatusBar();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_home_title = (TextView) findViewById(R.id.tv_home_title);
        tv_home_title.setText("我的足迹");


    }

    @Override
    public void onResume() {
        super.onResume();


        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.bt_summit:


                break;
        }
    }


    /**
     * 获取我的收藏
     */
    public void getFootActicles() {

        Map<String, String> params = new HashMap<>();
        params.put("page", "");
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", SharepUtils.getString_info(MyFootActivity.this, SharepUtils.USER_LOGIN_TOKEN));
        String url = NetConfig.APPUSER_FOOTARTICLES + "/" + SharepUtils.getString_info(MyFootActivity.this, SharepUtils.USER_LOGIN_USERID);

        OkHttpUtils.get()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        mRecyclerView.refreshComplete(10);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();

                        int num = 0;
                        MyFootBean packlist = gson.fromJson(response, MyFootBean.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            itemList = new ArrayList<MyFootBean.DatasBean.FootArticlesBean>();
                            itemList = packlist.getDatas().getFootArticles();
                            num = itemList.size();
                            if (num > 0) {
                                mRecyclerView.setVisibility(View.VISIBLE);
                                mEmptyView.setVisibility(View.GONE);
                                mDataAdapter.addAll(itemList);
                            } else {
                                tv_no_data.setText("暂无数据");

                                mRecyclerView.setVisibility(View.GONE);
                                mEmptyView.setVisibility(View.VISIBLE);
                            }
                            mRecyclerView.refreshComplete(num);
                        }


                    }
                });
    }

    /**
     * 获取偏好
     */
    public void getDeleteCollectActicles(String vid) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("Authorization", SharepUtils.getString_info(MyFootActivity.this, SharepUtils.USER_LOGIN_TOKEN));
        String url = NetConfig.APPUSER_DELETE_FOOTARTICLES
                + "/" + SharepUtils.getString_info(MyFootActivity.this, SharepUtils.USER_LOGIN_USERID)
                + "/delFootPrint/" + vid;

        OkHttpUtils.delete()
                .url(url)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        GsonResultok packlist = gson.fromJson(response, GsonResultok.class);
                        int code = packlist.getCode();
                        if (code == 200) {

                        }


                    }
                });
    }


}
